package jsyf.oa.service.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.system.RoleDao;
import jsyf.oa.dao.system.ShouldSignInDao;
import jsyf.oa.entity.system.ShouldSignInEntity;
import jsyf.oa.facade.system.ShouldSignInFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WHH-PC on 2018/2/5.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class ShouldSignInService implements ShouldSignInFacade {
    @Autowired
    RoleDao roleDao;

    @Autowired
    ShouldSignInDao shouldSignInDao;

    @Override
    public String save(String jsonData) {
        ShouldSignInEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), ShouldSignInEntity.class);
        List<JSONObject> roleList = new ArrayList<>();
        //删除一线岗位或后台岗位某年的应出勤天数配置
        shouldSignInDao.removeByCondition(" AND FRONT_FLAG = ? AND LEFT(SHOULD_MONTH,4) = ?",new String[]{entity.getFrontFlag(),entity.getYear()});
        if (entity.getFrontFlag().equals("1")) {
            //一线岗位角色
            roleList = roleDao.getJSONListByCondition(" AND ROLE_NAME_ID IN(SELECT ID FROM T_SYS_ROLE_NAME WHERE FRONT_FLAG='1') AND DEL_FLAG=0", null);

        } else if (entity.getFrontFlag().equals("0")) {
            //后台岗位角色
            roleList = roleDao.getJSONListByCondition(" AND ROLE_NAME_ID IN(SELECT ID FROM T_SYS_ROLE_NAME WHERE FRONT_FLAG='0') AND DEL_FLAG=0", null);
        }
        List<ShouldSignInEntity> shouldSignInList = new ArrayList<>();

        for (Integer i = 0; i < roleList.size(); i++) {
            String roleId = roleList.get(i).get("id").toString();
            ShouldSignInEntity addEntity1 = new ShouldSignInEntity();
            addEntity1.setRoleId(roleId);
            addEntity1.setShouldMonth(entity.getYear() + "-01");
            addEntity1.setShouldSignIn(entity.getMonthOne());
            addEntity1.setCreateUserId(entity.getCreateUserId());
            addEntity1.setCreateTime(entity.getCreateTime());
            addEntity1.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity1);

            ShouldSignInEntity addEntity2 = new ShouldSignInEntity();
            addEntity2.setRoleId(roleId);
            addEntity2.setShouldMonth(entity.getYear() + "-02");
            addEntity2.setShouldSignIn(entity.getMonthTwo());
            addEntity2.setCreateUserId(entity.getCreateUserId());
            addEntity2.setCreateTime(entity.getCreateTime());
            addEntity2.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity2);

            ShouldSignInEntity addEntity3 = new ShouldSignInEntity();
            addEntity3.setRoleId(roleId);
            addEntity3.setShouldMonth(entity.getYear() + "-03");
            addEntity3.setShouldSignIn(entity.getMonthThree());
            addEntity3.setCreateUserId(entity.getCreateUserId());
            addEntity3.setCreateTime(entity.getCreateTime());
            addEntity3.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity3);

            ShouldSignInEntity addEntity4 = new ShouldSignInEntity();
            addEntity4.setRoleId(roleId);
            addEntity4.setShouldMonth(entity.getYear() + "-04");
            addEntity4.setShouldSignIn(entity.getMonthFour());
            addEntity4.setCreateUserId(entity.getCreateUserId());
            addEntity4.setCreateTime(entity.getCreateTime());
            addEntity4.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity4);

            ShouldSignInEntity addEntity5 = new ShouldSignInEntity();
            addEntity5.setRoleId(roleId);
            addEntity5.setShouldMonth(entity.getYear() + "-05");
            addEntity5.setShouldSignIn(entity.getMonthFive());
            addEntity5.setCreateUserId(entity.getCreateUserId());
            addEntity5.setCreateTime(entity.getCreateTime());
            addEntity5.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity5);

            ShouldSignInEntity addEntity6 = new ShouldSignInEntity();
            addEntity6.setRoleId(roleId);
            addEntity6.setShouldMonth(entity.getYear() + "-06");
            addEntity6.setShouldSignIn(entity.getMonthSix());
            addEntity6.setCreateUserId(entity.getCreateUserId());
            addEntity6.setCreateTime(entity.getCreateTime());
            addEntity6.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity6);

            ShouldSignInEntity addEntity7 = new ShouldSignInEntity();
            addEntity7.setRoleId(roleId);
            addEntity7.setShouldMonth(entity.getYear() + "-07");
            addEntity7.setShouldSignIn(entity.getMonthSeven());
            addEntity7.setCreateUserId(entity.getCreateUserId());
            addEntity7.setCreateTime(entity.getCreateTime());
            addEntity7.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity7);

            ShouldSignInEntity addEntity8 = new ShouldSignInEntity();
            addEntity8.setRoleId(roleId);
            addEntity8.setShouldMonth(entity.getYear() + "-08");
            addEntity8.setShouldSignIn(entity.getMonthEight());
            addEntity8.setCreateUserId(entity.getCreateUserId());
            addEntity8.setCreateTime(entity.getCreateTime());
            addEntity8.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity8);

            ShouldSignInEntity addEntity9 = new ShouldSignInEntity();
            addEntity9.setRoleId(roleId);
            addEntity9.setShouldMonth(entity.getYear() + "-09");
            addEntity9.setShouldSignIn(entity.getMonthNine());
            addEntity9.setCreateUserId(entity.getCreateUserId());
            addEntity9.setCreateTime(entity.getCreateTime());
            addEntity9.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity9);

            ShouldSignInEntity addEntity10 = new ShouldSignInEntity();
            addEntity10.setRoleId(roleId);
            addEntity10.setShouldMonth(entity.getYear() + "-10");
            addEntity10.setShouldSignIn(entity.getMonthTen());
            addEntity10.setCreateUserId(entity.getCreateUserId());
            addEntity10.setCreateTime(entity.getCreateTime());
            addEntity10.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity10);

            ShouldSignInEntity addEntity11 = new ShouldSignInEntity();
            addEntity11.setRoleId(roleId);
            addEntity11.setShouldMonth(entity.getYear() + "-11");
            addEntity11.setShouldSignIn(entity.getMonthEleven());
            addEntity11.setCreateUserId(entity.getCreateUserId());
            addEntity11.setCreateTime(entity.getCreateTime());
            addEntity11.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity11);

            ShouldSignInEntity addEntity12 = new ShouldSignInEntity();
            addEntity12.setRoleId(roleId);
            addEntity12.setShouldMonth(entity.getYear() + "-12");
            addEntity12.setShouldSignIn(entity.getMonthTwelve());
            addEntity12.setCreateUserId(entity.getCreateUserId());
            addEntity12.setCreateTime(entity.getCreateTime());
            addEntity12.setFrontFlag(entity.getFrontFlag());
            shouldSignInList.add(addEntity12);
        }
        shouldSignInDao.insertBatch(shouldSignInList);
        return entity.getId();
    }

    @Override
    public List<JSONObject> getShouldSignInList(JSONObject jsonObject) {
        String year = jsonObject.get("year").toString();
        List<JSONObject> list = shouldSignInDao.getJSONListByCondition(" AND LEFT(SHOULD_MONTH,4) = ? group by SHOULD_MONTH,FRONT_FLAG", new Object[]{year});
        return list;
    }
}
